#ifndef YUVPLAYER_H
#define YUVPLAYER_H

#include <QWidget>
#include <QFile>
#include "yuvspec.h"


class YUVFileSpec:public YUVSpec{

private:

    char* yuvPath;

    void copyPath(const char* path) {

        int len = strlen(path) + 1;
        this->yuvPath = new char[len]{};
        memset(this->yuvPath,0,len);
        memcpy(this->yuvPath,path,len);
    }
public:
    YUVFileSpec(int width,int height,int fps,AVPixelFormat format,const char* yuvPath):
        YUVSpec(width,height,fps,format){

        copyPath(yuvPath);
    }
    YUVFileSpec(const YUVFileSpec& yuvSpec):YUVFileSpec(yuvSpec.getWidth(),yuvSpec.getHeight(),yuvSpec.getFps(),yuvSpec.getFormat(),yuvSpec.yuvPath){


    }

    ~YUVFileSpec() {

        if(yuvPath) {

            delete [] yuvPath;
            yuvPath = nullptr;
        }
    }


    const char* getYuvPath() {

        return yuvPath;
    }
};


class YUVPlayer : public QWidget
{
    Q_OBJECT
public:

    enum State{

        Playing,
        Stop,
        Pause
    };

    explicit YUVPlayer(QWidget *parent = nullptr);
    ~YUVPlayer();
    int init(YUVFileSpec& yuvSpec);
    void play();
signals:

private:
    YUVFileSpec* _yuvSpec = nullptr;
    State _state = Stop;
    QFile* _yuvFile = nullptr;
    char* _imageBuffer = nullptr;
    int _timer = 0;
    QImage* _paintImage = nullptr;


    void timerEvent(QTimerEvent* event);
    void paintEvent(QPaintEvent* event);
};

#endif // YUVPLAYER_H
